package com.lysj.bhp.blacklist.controller;

import com.lysj.bhp.blacklist.base.BlacklistContent;
import com.lysj.bhp.blacklist.domain.Blacklist;
import com.lysj.bhp.blacklist.vo.BlacklistVo;
import com.lysj.bhp.borrowing.base.BorrowingContent;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: ZhangWenJian
 * @Create: 2018/8/14--10:05
 * @Description:
 */
@RestController
@RequestMapping("admin/blacklist")
public class BlacklistController extends BlacklistContent {
    @RequestMapping("/list")
    public Resp list(Blacklist model){
        List<BlacklistVo> blacklistVos =blacklistRepository.findBlacklistVoList();
        return new Resp(blacklistVos);
    }

    @PostMapping("/remove")
    public Resp remove(Blacklist model) {
        //判断开始
        if(model.getBlacklistId() == null || ParamUtil.isBlack(model.getBlacklistId())){
            return new Resp(Resp.PARAM_ERROR,"请选择需要删除的黑名单记录");
        }
        //查找该ID，状态为正常的利率记录
        Blacklist blacklist = blacklistRepository.findByBlacklistIdAndIsExist(model.getBlacklistId(),1);
        if(blacklist == null){
            return new Resp(Resp.PARAM_ERROR,"该记录数据已经不存在！请联系管理员");
        }
        blacklist.setIsExist(2);
        blacklistRepository.save(blacklist);
        return new Resp("删除该用户黑名单成功！");
    }
    @PostMapping("/batch_remove")
    public Resp batchRemove(String ids) {
        String idList[] = ids.split(",");
        int failureAccount = 0;
        int successAccount = 0;
        for (int i=0;i<idList.length;i++){
            System.out.println(Long.parseLong(idList[i]));
            Blacklist blacklist = blacklistRepository.findByBlacklistIdAndIsExist(Long.parseLong(idList[i]),1);
            if(blacklist == null){
                failureAccount++;
            }
            else {
                blacklist.setIsExist(2);
                blacklistRepository.save(blacklist);
                successAccount++;
            }
        }
        if(failureAccount == 0){
            return new Resp("批量删除操作成功，总计操作"+idList.length+"条数据,"+successAccount+"条数据删除成功");
        }
        else{
            return new Resp("批量删除操作成功，总计操作"+idList.length+"条数据,"+successAccount+"条数据删除成功，"+failureAccount+"条数据由于黑名单不存在而删除失败");
        }
    }
}
